package edu.luc.concordia;

import java.util.ArrayList;

/**
 * Copyright 2008 Project Concordia
 * <p>
 * This file is part of RetailReturnSystem.
 * <p>
 * RetailReturnSystem is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * <p>
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <a
 * href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.
 * <hr>
 * Adds items to myInventory and stores as a temporary receipt.
 * 
 * @author Paul Zolnierczyk
 * @version 0.3 2008-10-07
 */

public class PurchaseRecorder {
	private Inventory myInventory;
	private ArrayList<Product> myScannedItems = new ArrayList<Product>();
	private ArrayList<Product> myScannedItemsC2 = new ArrayList<Product>();

	/*
	 * Class Constructor
	 */
	public PurchaseRecorder(Inventory inven) {
		myInventory = inven;
	}

	/**
	 * Scans an item into myScannedItems after verification product exists and
	 * product quantity greater than integer zero.
	 * 
	 * @param uid
	 *            - a String unique identification number that comes from the
	 *            scanner GUI.
	 * @param quantity
	 *            - an integer specifying the amount of the product to be added
	 *            to myScannedItems.
	 * @return scannedProductDesc - returns the description of the item scanned
	 *         to the Scanner GUI.
	 */
	public Product scanItem(String uid, int quantity) {
		Product myProduct = myInventory.getProduct(uid);

		if (myProduct != null && quantity > 0) {
			for (int i = 0; i < quantity; ++i) {
				myScannedItems.add(myProduct);
			}
			return myProduct;
		}
		return null;
	}

	/**
	 * Scans an item into myScannedItemsC2 after verification product exists and
	 * product quantity greater than integer zero.
	 * 
	 * @param uid
	 *            - a String unique identification number that comes from the
	 *            scanner GUI.
	 * @param quantity
	 *            - an integer specifying the amount of the product to be added
	 *            to myScannedItems.
	 * @param calculatedPrice
	 *            - a double value with the calculated price from price scheme.
	 * @return scannedProductDesc - returns the description of the item scanned
	 *         to the Scanner GUI.
	 */
	public ArrayList<Product> scanItemC2(String uid, int quantity) {
		Product myProduct = myInventory.getProduct(uid);

		if (myProduct != null && quantity > 0) {
			myProduct = new Product(myProduct);

			/* Cycle 2 */
			int purchaseCount = 0;
			double calculatedPrice = 0.0;

			// loop through myScannedItems
			for (int i = 0; i < myScannedItemsC2.size(); ++i) {
				// count number of times this item has been purchased
				String prodUID = myProduct.getUID();
				if (prodUID.equals(myScannedItemsC2.get(i).getUID())) {
					purchaseCount++;
				}
			}

			// get product price scheme
			ArrayList<PriceScheme> schemeList = myProduct.getPriceScheme();
			PriceScheme priceScheme = null;
			for (PriceScheme tempScheme : schemeList) {
				if (tempScheme.getType().equals("product")
						&& !tempScheme.isMember()) {
					priceScheme = tempScheme;
				}
			}

			ArrayList<Product> addedProducts = new ArrayList<Product>();
			for (int i = purchaseCount; i < purchaseCount + quantity; ++i) {
				calculatedPrice = priceScheme.calculateProductPrice(i + 1);
				if (Double.compare(calculatedPrice, PriceScheme.CAN_NOT_BUY) == 0) {
					return addedProducts;
				}
				myProduct.setCalculatedPrice(calculatedPrice);
				myScannedItemsC2.add(new Product(myProduct));
				addedProducts.add(new Product(myProduct));
			}

			// return arraylist of products with calculated prices
			return addedProducts;

			/* End Cycle 2 */
		}

		return null;
	}

	/**
	 * @return myScannedItems - an ArrayList of myScannedItems added by the
	 *         scanItem method.
	 */
	public ArrayList<Product> getScannedItems() {
		return (ArrayList<Product>) myScannedItems.clone();
	}

	/**
	 * @return myScannedItemsC2 - an ArrayList of myScannedItemsC2 added by the
	 *         scanItemC2 method.
	 */
	public ArrayList<Product> getScannedItemsC2() {
		return (ArrayList<Product>) myScannedItemsC2.clone();
	}

}
